import AxiosMethods from "@/util/AxiosMethods"
const state = {
  // 所有项目的列表
  allProjects: []
}

const getters = {}

const mutations = {
  InitAllProjects(state, projects){
    // 乱序
    projects.sort(() => Math.random() - 0.5)
    state.allProjects = projects
    // 为每一个allProjects中的photo属性前面添加地址
    state.allProjects.forEach(project => {
      project.photo = 'src/assets/images/' + project.photo
    })
  },
}

const actions = {
  // 获取所有项目列表
  async searchAllProjects({commit}){
    try{
      const response = await AxiosMethods.post('/project/select',{})
      if(response.data.code === 1){
        // console.log("所有项目列表获取成功！",response.data.data)
        commit('InitAllProjects', response.data.data)
      }
    }catch(error){
      console.log(error)
    }
  },
  //根据项目pid获取项目详情
  async getProjectByPid({commit}, pid){
    try{
      const response = await AxiosMethods.post('/project/select',{pid:pid})
      if(response.data.code === 1){
        //console.log("项目详情获取成功！",response.data.data[0])
        return response.data.data[0]
      }
    }catch(error){
      console.log(error)
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}